package org.example.rabbitmqspring.controller;

import org.example.rabbitmqspring.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 这个类写生产者的代码
 */
@RestController
@RequestMapping("/producer")
public class RabbitMQController {
    @Autowired
    RabbitTemplate template;

    //工作队列模式
    @RequestMapping("/work")
    @ResponseBody
    public String workQueue() {
        template.convertAndSend("", Constants.WORK_QUEUE, "我是工作队列模式下发来的消息");
        return "发送消息成功";
    }

    //广播模式
    @RequestMapping("publish")
    @ResponseBody
    public String fanoutProduct() {
        template.convertAndSend(Constants.FANOUT_EXCHANGE, "", "我是广播模式下发来的消息");
        return "发送消息成功";
    }

    //路由模式

    @RequestMapping("/direct/{routingKey}")
    @ResponseBody
    public String directProduct(@PathVariable("routingKey") String key) {
        template.convertAndSend(Constants.DIRECT_EXCHANGE, key, "我是路由模式下发来的消息");
        return "OK";
    }

    //topic模式
    @RequestMapping("/topic/{routingKey}")
    @ResponseBody
    public String topicProduct(@PathVariable("routingKey") String key) {
        template.convertAndSend(Constants.TOPIC_EXCHANGE, key, "我是topic模式下发来的消息");
        return "OK";
    }

}
